import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { ActivatedRoute, Data } from '@angular/router';
import { DataInput, SendData, Table, User } from '../model/data.model';
import { ProjectService } from '../project.service';

@Component({
  selector: 'app-homepage',
  templateUrl: './homepage.component.html',
  styleUrls: ['./homepage.component.scss']
})
export class HomepageComponent implements OnInit {
  user!: User;
  displayUserName: string = "";
  displayFunctionName: string = "";
  dataInput!: DataInput;
  userStockData!: Table;
  allStockData!: Data[];
  displayStockData: Table[] = [];
  sendData !: SendData;

  addStockForm = new FormGroup({
    nums: new FormControl(''),
    stockIds: new FormControl('')
  });
  constructor(
    private http: HttpClient,
    private route: ActivatedRoute,
    private projectService: ProjectService
  ) { }

  ngOnInit(): void {
    this.displayUserName = history.state.user_username;
    this.displayUserName =window.localStorage["displayUserName"];
    this.getDisplayFunctionName();
  }

  getDisplayFunctionName() {
    this.displayFunctionName = String(this.route.snapshot.paramMap.get('name'));
  }

  getStockById(id: string) {
    this.projectService.getStockById(id)
      .subscribe(
        res => {
          this.allStockData=res;
          console.log("getStockById",res);
        }
      );
  }

  // addStock(nums: string[]) {
  // //   const nums: string[] = ["1000", "0.12"];
  // //   const stockIds: string[] = ["000001.SZ", "000002.SH"];
  // //   const userId: string = this.displayUserName;
  //   let tempResult2: any;
  //   this.projectService.addStock(nums, stockIds, userId)
  //     .subscribe(
  //       res => {
  //         tempResult2 = res;
  //         console.log(res)
  //       }
  //     )
  // }

  onSubmit() {
    this.dataInput = this.addStockForm.value;
    const nums: string[] =[];
    for(let i=0;i<this.dataInput.nums.length-1;i++){
      if(this.dataInput.nums.split(",")[i] != undefined)
      nums[nums.length]=this.dataInput.nums.split(",")[i];
    }
    const stockIds: string[] =[];
    for(let i=0;i<this.dataInput.stockIds.length-1;i++){
      if(this.dataInput.stockIds.split(",")[i] != undefined)
      stockIds[stockIds.length]=this.dataInput.stockIds.split(",")[i];
    }
    const userId: string = this.displayUserName;
    this.sendData = {
      nums: nums,
      stockIds: stockIds,
      userId :userId
    }
    this.sendData.nums=nums;
    this.sendData.stockIds=stockIds;
    this.sendData.userId=userId;
    this.showUserStockTable(stockIds,nums);
  }

  showUserStockTable(stockIds:string[],nums:string[]) {
    let stockCode : string[] =[];
    let stockNum : string[] =[];
    stockCode = stockIds;
    stockNum = nums;
    let index= this.displayStockData.length
    for(let i=0;i<stockCode.length;i++) {
        let stockData:Table ={
          stockIds: stockCode[i],
          nums: Number(stockNum[i])

      }
      this.displayStockData[index+i]=stockData;
    }
  }

  deleteInfo(stockCode:string) {
    for(let i= 0;i <this.displayStockData.length;i++) {
      if(this.displayStockData[i]['stockIds']==stockCode) {
        this.displayStockData.splice(i,1);
      }
    }
  }

  send(){
    this.projectService.addStock(this.sendData.nums, this.sendData.stockIds, this.sendData.userId)
    .subscribe(
      res => {
        console.log("addStock",res)
      }
    )
  }
}
